using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace GMailNotifierNET.Classes.Animations
{
    public class FadeAnimation : AnimationBase
    {
        private System.Timers.Timer m_timer = null;
        private double m_dblStartOpacity = 0.0;
        private double m_dblDesiredOpacity = 1.0;
        private FadeDirection m_direction = FadeDirection.FadeIn;
        private bool m_boolUseCurrentOpacityAsStartValue = false;

        public override event Animations.AnimationFinishedDelegate AnimationFinished;
        public override event AnimationBase.AnimationStepChangedDelegate AnimationStepChanged;


        public enum FadeDirection
        {
            FadeIn,
            FadeOut
        }

        public FadeAnimation(int duration, double startOpacity, double endOpacity) 
            : base(duration)
        {
            this.m_dblStartOpacity = startOpacity;
            this.m_dblDesiredOpacity = endOpacity;
        }

        public FadeAnimation(int duration, double endOpacity)
            : base(duration)
        {
            this.m_boolUseCurrentOpacityAsStartValue = true;
            this.m_dblDesiredOpacity = endOpacity;
        }

        public FadeAnimation(int duration, FadeDirection direction)
            : base(duration)
        {
            this.m_boolUseCurrentOpacityAsStartValue = true;
            this.m_direction = direction;

            switch (direction)
            { 
                case FadeDirection.FadeIn:
                    this.m_dblDesiredOpacity = 1.0;
                    break;

                case FadeDirection.FadeOut:
                    this.m_dblDesiredOpacity = 0.0;
                    break;            
            }
        }

        public override void StartAnimation(Form form)
        {
            this.m_formToAnimate = form;

            if (this.m_boolUseCurrentOpacityAsStartValue)
                this.m_dblStartOpacity = this.m_formToAnimate.Opacity;
            else
                this.m_formToAnimate.Opacity = this.m_dblStartOpacity;

            this.m_datStartTime = DateTime.Now;
            this.m_datStopTime = this.m_datStartTime.AddMilliseconds(this.m_intDuration);

            this.m_timer = new System.Timers.Timer(1.0);
            this.m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
            this.m_timer.Start();

            this.m_formToAnimate.Visible = true;
        }

        private void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            double currentPercent = this.CalculateCurrentStepPercent(
                this.m_datStartTime, 
                this.m_datStopTime, 
                DateTime.Now);
            
            if (currentPercent > 0.0 && currentPercent < 1.0)
            {
                this.SetOpacity(
                    this.m_formToAnimate, 
                    this.m_dblStartOpacity + (this.m_dblDesiredOpacity - this.m_dblStartOpacity) * currentPercent);

                if (this.AnimationStepChanged != null)
                    this.AnimationStepChanged(this, new AnimationStepEventArgs(currentPercent));
            }
            else if (currentPercent >= 1.0)
            {
                this.SetOpacity(this.m_formToAnimate, this.m_dblDesiredOpacity);

                if (this.AnimationFinished != null)
                    this.AnimationFinished(this, EventArgs.Empty);
                
                this.m_timer.Elapsed -= new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
            }
        }
    }
}
